package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.server.service.PublishInfoServicce;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.PageResultVo;
import com.tanhua.server.vo.PublishInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("management/manage")
@Slf4j
public class PublishInfoController {

    @Autowired
    private PublishInfoServicce publishInfoServicce;

    @GetMapping("/messages")
    @NoAuthorization
    public PageResultVo queryAll(Integer page, Integer pagesize, String sortProp, String sortOrder, String id, Long sd, Long ed, String uid, String state) {
        try {
            PageResultVo pageResultVo = this.publishInfoServicce.queryAll(page, pagesize, sortProp, sortOrder, id, sd, ed, uid, state);
            if (ObjectUtil.isNotEmpty(pageResultVo)) {
                return pageResultVo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过
     *
     * @return
     */
    @PostMapping("/messages/pass")
    @NoAuthorization
    public ResponseEntity<String> passPublishInfo(@RequestBody List<String> id) {
        try {
            String message = this.publishInfoServicce.passPublishInfo(id);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("审核出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 不通过
     *
     * @return
     */
    @PostMapping("/messages/reject")
    @NoAuthorization
    public ResponseEntity<String> rejectPublishInfo(@RequestBody List<String> id) {
        try {
            String message = this.publishInfoServicce.rejectPublishInfo(id);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("审核出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 撤销通过
     *
     * @return
     */
    @PostMapping("/messages/revocation")
    @NoAuthorization
    public ResponseEntity<String> revokePublishInfo(@RequestBody List<String> id) {
        try {
            String message = this.publishInfoServicce.revokePublishInfo(id);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("审核出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息置顶
     *
     * @return
     */
    @PostMapping("/messages/{id}/top")
    @NoAuthorization
    public ResponseEntity<String> top(@PathVariable String id) {
        try {
            String message = this.publishInfoServicce.top(id);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消消息置顶
     *
     * @return
     */
    @PostMapping("/messages/{id}/untop")
    @NoAuthorization
    public ResponseEntity<String> untop(@PathVariable String id) {
        try {
            String message = this.publishInfoServicce.untop(id);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息详情
     * @param id
     * @return
     */
    @GetMapping("/messages/{id}")
    @NoAuthorization
    public ResponseEntity<PublishInfoVo> queryPublishInfo(@PathVariable String id) {
        try {
            PublishInfoVo publishInfoVo = this.publishInfoServicce.queryPublishInfo(id);
            return ResponseEntity.ok(publishInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息翻页
     * @param pagesize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param messageID
     * @return
     */
    @GetMapping("/messages/comments")
    @NoAuthorization
    public ResponseEntity<PageResult> queryComment(Integer pagesize, Integer page, String sortProp, String sortOrder, String messageID) {
        try {
            PageResult pageResult = this.publishInfoServicce.queryComment(pagesize, page, sortProp, sortOrder, messageID);
            if (ObjectUtil.isNotEmpty(pageResult)){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
